{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 列表常用方法、操作\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '12ab', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 创建一个混合列表\n",
    "my_list = [1, 1.0, '12ab', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.163816Z",
     "end_time": "2024-07-02T16:06:14.171632Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 修改某个元素\n",
    "my_list[2] = '123'\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.172630Z",
     "end_time": "2024-07-02T16:06:14.179772Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 在列表指定位置插入元素\n",
    "my_list.insert(2, 'inserted')\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.179772Z",
     "end_time": "2024-07-02T16:06:14.187231Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}, 'tail']\n"
     ]
    }
   ],
   "source": [
    "# 在列表尾部插入元素\n",
    "my_list.append('tail')\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.185521Z",
     "end_time": "2024-07-02T16:06:14.247176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 通过索引删除\n",
    "del my_list[-1]\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.192243Z",
     "end_time": "2024-07-02T16:06:14.262176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 删除某个元素\n",
    "my_list.remove('123')\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.197863Z",
     "end_time": "2024-07-02T16:06:14.262176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No\n"
     ]
    }
   ],
   "source": [
    "# 判断一个元素是否在列表中\n",
    "if '123' in my_list:\n",
    "    print(\"Yes\")\n",
    "else:\n",
    "    print(\"No\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.203830Z",
     "end_time": "2024-07-02T16:06:14.262176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{1: 1.0}, {1}, [1, 1.0, '1'], True, 'inserted', 1.0, 1]\n"
     ]
    }
   ],
   "source": [
    "# 列表翻转\n",
    "my_list.reverse()\n",
    "print(my_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.209429Z",
     "end_time": "2024-07-02T16:06:14.262176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J_a_m_e_s\n"
     ]
    }
   ],
   "source": [
    "# 将列表用所有字符连接，连接符为下划线 _\n",
    "letters = ['J', 'a', 'm', 'e', 's']\n",
    "word = '_'.join(letters)\n",
    "print(word)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.215252Z",
     "end_time": "2024-07-02T16:06:14.262176Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T16:06:14.220986Z",
     "end_time": "2024-07-02T16:06:14.263176Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
